<?php

namespace McUser\Model;
use Common\Model\CommonModel;

/*
 *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

class WalletTiXianModel extends CommonModel {

	protected $trueTableName = 'bg_wallet_tixian';
	
	/*
	*	读取提现历史
	*  $flag: 0-> 新的; 1-> 操作完成的.
	*/
	public function read_tixian_his($userID, $flag){

		$where['userID'] = $userID;
		/* $where['tiXianFlag'] = $flag; */

		$data = $this -> where($where) -> order('ID desc') -> select();
		
		return $data;
	}

	/*
	*	检查提现时间: 一个礼拜之内只能提现一次.
	*/
	public function check_apply_valid($userID){
		$where['userID'] = $userID;
		
		$data = $this -> where($where) -> find();
		if( $data == null)
			return '1';

		/*   申请时间不能同一个礼拜   */
		$tDate = $data['applyDatetime'];

		$wLast = date('W',strtotime($tDate));
		$wNow = date('W',time());
		if( $wLast == $wNow )
			return '-301';

		if( $data['tiXianFlag'] == '0' )
			return '-302';

		return '1';

	}

	/*
	*	检查上次提现是否完成
	*/
	public function check_if_completed($userID){
		$where['userID'] = $userID;
		
		$flag = $this -> where($where) -> getField('tiXianFlag');	

		if ( $flag == null || $flag =='1' )
			return true;
		else
			return false;
	}



	/*
	*	生成随机字符串
	*/
	function getRandChar($length){
	   $str = null;
	   $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
	   $max = strlen($strPol)-1;

	   for($i=0;$i<$length;$i++){
	    	$str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
	   }

	   return $str;
  	}


	/*
	*	添加提现申请
	*/
	public function add_apply_for_cash($userID, $applyCash){
		$tDate =  date('Y-m-d H:i:s', time());

		$data['userID'] = $userID;
		$data['applyDatetime'] = $tDate ;   // date('Y-m-d H:i:s', time());
		$data['tiXianJinE'] = $applyCash;
		$data['tiXianFlag'] = 0;

		/*  生成随机字符串, tiXianID  */
		$data['tiXianID'] = $this -> getRandChar(8);
		
		$this -> add($data) ;	
	}

	/*
	*	提现完成
	*/
	public function apply_cash_opr($userID, $tiXianJinE, $tiXianID){
		$where['userID'] = $userID;
		$where['tiXianID'] = $tiXianID;

		$code = C('retJson');
		$list = $this -> where($where) -> order('ID desc') -> select();
		/* dump($list); */
		if ( count($list) == 0)		{
			/* 没找到提现信息 */
			$rejson = array(
				"retCode" => "-311",
				"retMsg" => $code['-311'] );
			return $rejson;
		}

		$tiXianFlag = $list[0]['tiXianFlag'];
		if ( $tiXianFlag == '1' ){
			/*  重复提现  */
			$rejson = array(
				"retCode" => "-313",
				"retMsg" => $code['-313'] );
			return $rejson;	
		}

		$jinE = $list[0]['tiXianJinE'];
		if ( $tiXianJinE != $jinE ){
			/*  提现金额不对  */
			$rejson = array(
				"retCode" => "-312",
				"retMsg" => $code['-312'] );
			return $rejson;	
		}

		/*  保存  */
		$data['oprDatetime'] = date('Y-m-d H:i:s', time());
		$data['tiXianFlag'] = '1';
		$this -> where($where) -> save($data);
		
		/*    */
			$rejson = array(
				"retCode" => "1",
				"retMsg" => $code['1'] );
			return $rejson;	
	}

}